﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.IO.Ports;
using System.Data.SqlClient;
using dateDealNamespace;
using System.Threading;
using GDIPlusDemo;

namespace serialPortNamespace
{
    public class serialPort
    {
        DAL.DBbase d_DBbase = new DAL.DBbase();
        static string toController; 

        //上位机发送从串口发送指令给控制节点，指令说明插入数据库
        //参数说明：area 区域， command 控制指令, instruction 指令说明 
        //        node 节点编号(设为空，以后方便修改),from 指令来自PC、APP、WEB端
        public void buildOrder(string modeId,string from, string area, string command, string instruction, string node)                                                                                                                                                                                                                                                                                                                                                                                                                 
        {
            Thread.Sleep(1000);
            dataDeal myDataDealSerialPort = new dataDeal();
            string facNo = command.Substring(0, 2);
            string facSwitch,facSwitchChange;
            string sqlcomm = "INSERT INTO actionInstruction(modeId,orderFrom,area,facilitiesNo,mode,orderSend,instruction,actionTime) VALUES('" + modeId + "','" + from + "','" + area + "','" + node + "','" + "AUTO" + "','" + command + "','" + instruction + "',GETDATE())";            
            //string msg = "";
            //MessageBox.Show(sqlcomm);            
            //string sql = "INSERT INTO orderSend(area,facilitiesNo,fan,machine,waterPump,actionTime) VALUES("+area+","+facNo+""")"
            if (int.Parse(area) < 10)
            {
                area = "0" + area;
            }
            if (int.Parse(node) < 10)
            {
                node = "0" + node;
            }
            //switch (command)
            //{
            //    case "M10": msg = "风扇关"; break;
            //    case "M11": msg = "风扇开"; break;
            //    case "M20": msg = "加热灯关"; break;
            //    case "M21": msg = "加热灯开"; break;
            //    case "M30": msg = "水泵关"; break;
            //    case "M31": msg = "水泵开"; break;
            //    case "M40": msg = "天窗关"; break;
            //    case "M41": msg = "天窗开"; break;
            //    case "M50": msg = "白炽灯关"; break;
            //    case "M51": msg = "白炽灯开"; break;
            //}
            facSwitch = myDataDealSerialPort.chkFacFile(modeId, facNo, int.Parse(area));
            facSwitchChange = command.Substring(2, 1);
            //如果当前设备开启状态与指令状态不一样则赋值
            // if(0 != 1)
            if (facSwitch != facSwitchChange)
            {
                //toController = "AAE" + area + "F" + node + command + "55";
                toController = "AAE" + area + "F01" + command + "55";
                //MessageBox.Show("自动模式：\n自动模式编号："+modeId+"\n来自：" + from + "\n区域：" + area + msg + toController);
                d_DBbase.ExecuteNonQuery(sqlcomm);     //指令说明插入数据库

                //更新设备开启状态                
                myDataDealSerialPort.updateDb(facSwitchChange,modeId,area,facNo);
            }
            else
            {
                toController = null;
            }            
        }
        public static string toControllerNew()
        {
            return toController;
        }

      
        /**********添加手动模式2014-08-22*************************/
        //手动模式控制，上位机发送控制指令给控制节点，插入数据库
        //参数说明：area 区域， command 控制指令, instruction 指令说明 
        //facNO 设备号(设为空，以后方便修改),from 指令来自PC、APP、WEB端
        public bool manSendOrder(string modeId,string from, string area, string command, string instruction, string facNo)
        {
            Thread.Sleep(1000);
            dataDeal manDataDealSerialPort = new dataDeal();
            string sqlcomm = "INSERT INTO actionInstruction(modeId,orderfrom,area,facilitiesNo,mode,orderSend,instruction,actionTime) VALUES('" + modeId + "','" + from + "','" + area + "','" + facNo + "','" + "MAN" + "','" + command + "','" + instruction + "',GETDATE())";
            //MessageBox.Show(sqlcomm);
            try
            {
                if (int.Parse(area) < 10)
                {
                    area = "0" + area;
                }

                if (int.Parse(facNo) < 10)
                {
                    //facNO 设备号(设为01，以后方便修改)
                    facNo = "0" + facNo;
                }                
                
            }
            catch
            {
                MessageBox.Show("请选择区域号！");
            }
            d_DBbase.ExecuteNonQuery(sqlcomm);
            return true;
        }      
    }
}
